<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="format-detection" content="telephone=no" />
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <title>title</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
    <style>
        html, body {
            background-color: #f8f8f8;
        }
        .current_box {
        	height: 50px;
        	line-height: 50px;
        	display: -webkit-box;
        	display: -webkit-flex;
        	align-items: center;
        	padding: 0 15px;
        	background-color: #fff;
        	margin-bottom: 6px;
        }
        .current_box .current_boxl {
        	padding-right: 10px;
        	color: #333;
        	font-size: 15px;
        }
        .current_box .current_boxr {
        	width: 0;
        	flex: 1;
        	-webkit-flex: 1;
        	box-flex: 1;
        	-webkit-box-flex: 1;
        	font-size: 12px;
        	color: #999;
        	text-align: right;
        }
        .phone_box {
        	height: 50px;
        	line-height: 50px;
        	display: -webkit-box;
        	display: -webkit-flex;
        	align-items: center;
        	background-color: #fff;
        	border-bottom: 1px solid #e8e8e8;
        }
        .phone_box .phone_boxl {
        	width: 0;
        	height: 50px;
        	flex: 1;
        	-webkit-flex: 1;
        	box-flex: 1;
        	-webkit-box-flex: 1;
        	position: relative
        }
        .phone_box .phone_boxl .line {
        	width: 1px;
        	height: 28px;
        	background-color: #c4c4c4;
        	position: absolute;
        	right: 0;
        	top: 0;
        	bottom: 0;
        	margin: auto;
        }
        .phone_box .phone_boxl input {
        	width: 100%;
        	height: 100%;
        	color: #999;
        	font-size: 12px;
        	padding: 0 15px;
        	display: block;
        	box-sizing: border-box;
        }
        .phone_box .phone_boxl input::-webkit-input-placeholder {
        	color: #999;
        }
        .phone_box .phone_boxr .verify_btn {
        	width: 120px;
        	height: 100%;
        	font-size: 15px;
        	color: #21a9ff;
        	text-align: center;
        }
        .verify_box {
        	height: 50px;
        	line-height: 50px;
        	background-color: #fff;
        }
        .verify_box input {
        	width: 100%;
        	height: 100%;
        	color: #999;
        	padding: 0 15px;
        	font-size: 12px;
        	box-sizing: border-box;
        	display: block;
        }
        .verify_box input::-webkit-input-placeholder {
        	color: #999;
        }
        .btn_box {
			padding: 0 15px;
			margin-top: 50px;
        }
        .btn_box .btn {
        	width: 100%;
        	height: 44px;
        	line-height: 44px;
        	text-align: center;
        	color: #fff;
        	background-color: #e62442;
        	border-radius: 5px;
        	font-size: 15px;
        }
    </style>
</head>
<body>
	<div class="current_box">
		<div class="current_boxl">当前手机号</div>
		<div class="current_boxr my_phone"></div>
	</div>
	<div class="phone_box">
		<div class="phone_boxl">
			<input type="tel" class="new_phone" placeholder="请填写新手机号">
			<div class="line"></div>
		</div>
		<div class="phone_boxr" tapmode onclick="getCode();">
			<div class="verify_btn">获取验证码</div>
		</div>
	</div>
	<div class="verify_box">
		<input type="text" class="my_code" placeholder="请输入验证码">
	</div>
	<div class="btn_box" tapmode onclick="toConfirm();">
		<div class="btn">确认更换</div>
	</div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
    var canGet = true; // 默认可以获取验证码

    apiready = function(){
        staffData = $api.getStorage('staff_data');
        if(staffData && staffData.phone){
            $(".my_phone").html(staffData.phone);
        }

    };

    function getCode () {
        $("input").blur();
        if(canGet){
            var new_phone = $(".new_phone").val();
            var my_phone = $(".my_phone").html();
            if (isMobile(new_phone) == 0) {
                toast('手机号格式错误');
            }else if(my_phone == new_phone){
                toast('新手机与旧手机号相同');
            }else{
                toGetCode();
            }
        }
    };

    function toGetCode () {
        // 倒计时
        countdown();
        ajaxGetCode();
    };

    function ajaxGetCode () {
        var new_phone = $(".new_phone").val();
        api.ajax({
            url: rootPath + '/api/login/get_code',
            method: 'post',
            data: {
                values: { 
                    phone: new_phone,
                    type : 'staff_register'
                },
            }
        }, function(ret, err) {
            if(ret) {
                if(ret.status == 200) {
                   api.toast({
                       msg: '发送成功',
                       location : 'middle'
                   });
                    
                }else {
                    api.toast({
                        msg: '发送失败',
                        location: 'middle'
                    });
                }
            }else {
                toast(JSON.stringify(err))
            }
        });
    };

    function countdown () {
        canGet = false;
        $(".verify_btn").html('60s');
        var t = 60;
        var timer = setInterval(function(){
            t--;
            if(t == 0){
                $(".verify_btn").html('获取验证码');
                clearInterval(timer);
                canGet = true;
            }else{
                $(".verify_btn").html(t+'s');
            }
        },1000);
    };

    function toConfirm () {
        $("input").blur();
        openLoading();
        var new_phone = $(".new_phone").val();
        var my_code = $(".my_code").val();
        if(!new_phone){
            toast('手机号不能为空');
            return;
        }
        if(!my_code){
            toast('验证码不能为空');
            return;
        }
        if (isMobile(new_phone) == 0) {
            toast('手机号格式错误');
            return;
        }
        api.ajax({
            url: rootPath+'/api/staff/change_phone',
            method: 'post',
            data: {
                values: { 
                    staff_id: staffData.id,
                    staff_token: staffData.token,
                    new_phone: new_phone,
                    code: my_code
                },
            }
        }, function(ret, err) {
            if (ret) {
                if(ret.status == 200){
                    toast('更换成功');
                    staffData.phone = new_phone;
                    $api.setStorage('staff_data',staffData);
                    $api.setStorage('temp_phone', new_phone);
                    api.sendEvent({
                        name: 'changeStaffPhone',
                        extra: {
                            new_phone: new_phone, 
                        }
                    });
                    closeLoading();
                    setTimeout(function(){
                        api.closeWin();
                    },300);
                }else{
                    toast('验证码不正确');
                    closeLoading();
                }
            } else {
                closeLoading();
            }
        });
    };

</script>
</html>

